﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace FrozenBubble
{
    public class MenuScreen : GameScreen
    {
#if WINDOWS
        private int selectedMenuIndex;
#endif

        private List<MenuEntry> menuEntries;

        protected Texture2D BackGround { get; set; }

#if WINDOWS_PHONE
        private MouseState _oldInputState;
#else
        private KeyboardState _oldInputState;
#endif

        public MenuScreen()
        {
            menuEntries = new List<MenuEntry>();
        }

        /// <summary>
        /// Gets the list of menu entries, so derived classes can add
        /// or change the menu contents.
        /// </summary>
        protected IList<MenuEntry> Controls
        {
            get { return menuEntries; }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = ScreenManager.SpriteBatch;

            if(BackGround != null)
                sb.Draw(BackGround, ScreenManager.ViewportRectangle, Color.White);

            // Draw menu entries
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuEntry entry = menuEntries[i];
                sb.Draw(entry.Texture, entry.Position, Color.White);
            }
        }

        public override void  HandleInput(GameTime gameTime)
        {
#if WINDOWS_PHONE
            HandleTouch();
#else
            HandleKeyboard();
#endif
        }

#if WINDOWS_PHONE
        private void HandleTouch()
        {
            // We are using the Mouse code, this works on both Windows and Windows Phone 7.
            // We don't use the multi touch because we don't need this for a menu system
            MouseState mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed && _oldInputState.LeftButton == ButtonState.Released)
            {
                // There was a click, see if a button was pressed.
                foreach (MenuEntry entry in Controls)
                {
                    Vector2 pos = entry.Position;
                    if (pos.X < mouseState.X && mouseState.X < pos.X + entry.Texture.Width &&
                        pos.Y < mouseState.Y && mouseState.Y < pos.Y + entry.Texture.Height)
                    {
                        // This button was pressed, execute the click handler
                        entry.DoClick(this);
                    }
                }
            }

            _oldInputState = mouseState;
        }
#endif

#if WINDOWS
        private void HandleKeyboard()
        {
            KeyboardState inputState = Keyboard.GetState();

            if (inputState.IsKeyUp(Keys.Down) && _oldInputState.IsKeyDown(Keys.Down) && selectedMenuIndex < (Controls.Count - 1))
            {
                Controls[selectedMenuIndex].DoUnHover(this);
                selectedMenuIndex++;
                Controls[selectedMenuIndex].DoHover(this);
            }
            else if (inputState.IsKeyUp(Keys.Up) && _oldInputState.IsKeyDown(Keys.Up) && selectedMenuIndex > 0)
            {
                Controls[selectedMenuIndex].DoUnHover(this);
                selectedMenuIndex--;
                Controls[selectedMenuIndex].DoHover(this);
            }
            else if (
                ((inputState.IsKeyUp(Keys.Space) && _oldInputState.IsKeyDown(Keys.Space)) ||
                (inputState.IsKeyUp(Keys.Enter) && _oldInputState.IsKeyDown(Keys.Enter))) && Controls.Count > 0)
                Controls[selectedMenuIndex].DoClick(this);

            _oldInputState = inputState;
        }
#endif
    }
}
